#include "stdafx.h"
#include "IBlur.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const IBlur::FunctionNameIBlur = "Blur";

IBlur::IBlur(void) 
{
	_fnApi.SetFunctionName(IBlur::FunctionNameIBlur);
	_fnApi.SetFunctionDescription("This functionn blurs the image");
									//inputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, "KernelSizeW", "Sets the smoothing kernel size (Width)", ProcParamDef::input, 0, 100 ));
	_fnApi.AddParamDef(new ProcParamDefInt(1, "KernelSizeH", "Sets the smoothing kernel size (Height)", ProcParamDef::input, 0, 100 ));
									//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


IBlur::~IBlur(void)
{

}


bool IBlur::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		
		ProcParamInt *KernelSizeW = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(0));	//Check the Param number
		ProcParamInt *KernelSizeH = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(1));
		if  (KernelSizeW != '\0' && KernelSizeH != '\0')
		{
			cv::Mat output;
			cv::Mat input = cv::imread(step->GetIntermediateImagePathFilename());
			cv::Size ksize = cv::Size((int)(KernelSizeW->GetValue()), (int)(KernelSizeH->GetValue()));
			cv::blur(input, output, ksize);

			cv::imwrite(step->GetIntermediateImagePathFilename(), output);

			input.release();
			output.release();
			//result->SetValue(0);
			rc = true;
		}
	}
	return rc;
}

